#!/usr/bin/env python3
#
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
# Built on top of Unicorn emulator (www.unicorn-engine.org)

# cols = ("x8664")

from qiling.const import *

def map_syscall(ql, syscall_num):
    for k,v in syscall_table.items():
        
        if ql.archtype == QL_ARCH.X8664 and v == syscall_num:
            return "ql_syscall_" + k

syscall_table = {
    "nosys": (0),
    "exit": (1),
    "fork": (2),
    "read": (3),
    "write": (4),
    "open": (5),
    "close": (6),
    "wait4": (7),
    "creat": (8),
    "link": (9),
    "unlink": (10),
    "chdir": (12),
    "fchdir": (13),
    "mknod": (14),
    "chmod": (15),
    "chown": (16),
    "*break": (17),
    "getfsstat": (18),
    "lseek": (19),
    "getpid": (20),
    "mount": (21),
    "unmount": (22),
    "setuid": (23),
    "getuid": (24),
    "geteuid": (25),
    "ptrace": (26),
    "recvmsg": (27),
    "sendmsg": (28),
    "recvfrom": (29),
    "accept": (30),
    "getpeername": (31),
    "getsockname": (32),
    "access": (33),
    "chflags": (34),
    "fchflags": (35),
    "sync": (36),
    "kill": (37),
    "stat": (38),
    "getppid": (39),
    "lstat": (40),
    "dup": (41),
    "pipe": (42),
    "getegid": (43),
    "profil": (44),
    "ktrace": (45),
    "sigaction": (46),
    "getgid": (47),
    "sigprocmask": (48),
    "getlogin": (49),
    "setlogin": (50),
    "acct": (51),
    "sigpending": (52),
    "sigaltstack": (53),
    "ioctl": (54),
    "reboot": (55),
    "revoke": (56),
    "symlink": (57),
    "readlink": (58),
    "execve": (59),
    "umask": (60),
    "chroot": (61),
    "fstat": (62),
    "getkerninfo": (63),
    "getpagesize": (64),
    "msync": (65),
    "vfork": (66),
    "sbrk": (69),
    "sstk": (70),
    "*mmap": (71),
    "vadvise": (72),
    "munmap": (73),
    "mprotect": (74),
    "madvise": (75),
    "mincore": (78),
    "getgroups": (79),
    "setgroups": (80),
    "getpgrp": (81),
    "setpgid": (82),
    "setitimer": (83),
    "wait": (84),
    "swapon": (85),
    "getitimer": (86),
    "gethostname": (87),
    "sethostname": (88),
    "getdtablesize": (89),
    "dup2": (90),
    "fcntl": (92),
    "select": (93),
    "fsync": (95),
    "setpriority": (96),
    "socket": (97),
    "connect": (98),
    # "accept": (99),
    "getpriority": (100),
    "send": (101),
    "recv": (102),
    "sigreturn": (103),
    "bind": (104),
    "setsockopt": (105),
    "listen": (106),
    "sigvec": (108),
    "sigblock": (109),
    "sigsetmask": (110),
    "sigsuspend": (111),
    "sigstack": (112),
    # "recvmsg": (113),
    # "sendmsg": (114),
    "gettimeofday": (116),
    "getrusage": (117),
    "getsockopt": (118),
    "readv": (120),
    "writev": (121),
    "settimeofday": (122),
    "fchown": (123),
    "fchmod": (124),
    # "recvfrom": (125),
    "setreuid": (126),
    "setregid": (127),
    "rename": (128),
    "truncate": (129),
    "ftruncate": (130),
    "flock": (131),
    "mkfifo": (132),
    "sendto": (133),
    "shutdown": (134),
    "socketpair": (135),
    "mkdir": (136),
    "rmdir": (137),
    "utimes": (138),
    "adjtime": (140),
    # "getpeername": (141),
    "gethostid": (142),
    "sethostid": (143),
    "getrlimit": (144),
    "setrlimit": (145),
    "killpg": (146),
    "setsid": (147),
    "quotactl": (148),
    "quota": (149),
    # "getsockname": (150),
    "nlm_syscall": (154),
    "nfssvc": (155),
    "getdirentries": (156),
    "statfs": (157),
    "fstatfs": (158),
    "lgetfh": (160),
    "getfh": (161),
    "getdomainname": (162),
    "setdomainname": (163),
    "uname": (164),
    "sysarch": (165),
    "rtprio": (166),
    "semsys": (169),
    "msgsys": (170),
    "shmsys": (171),
    "pread": (173),
    "pwrite": (174),
    "setfib": (175),
    "ntp_adjtime": (176),
    "setgid": (181),
    "setegid": (182),
    "seteuid": (183),
    # "stat": (188),
    # "fstat": (189),
    # "lstat": (190),
    "pathconf": (191),
    "fpathconf": (192),
    # "getrlimit": (194),
    # "setrlimit": (195),
    # "getdirentries": (196),
    # "*mmap": (197),
    # "nosys": (198),
    # "lseek": (199),
    # "truncate": (200),
    # "ftruncate": (201),
    "__sysctl": (202),
    "mlock": (203),
    "munlock": (204),
    "undelete": (205),
    "futimes": (206),
    "getpgid": (207),
    "poll": (209),
    "__semctl": (220),
    "semget": (221),
    "semop": (222),
    "msgctl": (224),
    "msgget": (225),
    "msgsnd": (226),
    "msgrcv": (227),
    "*shmat": (228),
    "shmctl": (229),
    "shmdt": (230),
    "shmget": (231),
    "clock_gettime": (232),
    "clock_settime": (233),
    "clock_getres": (234),
    "ktimer_create": (235),
    "ktimer_delete": (236),
    "ktimer_settime": (237),
    "ktimer_gettime": (238),
    "ktimer_getoverrun": (239),
    "nanosleep": (240),
    "ffclock_getcounter": (241),
    "ffclock_setestimate": (242),
    "ffclock_getestimate": (243),
    "clock_nanosleep": (244),
    "clock_getcpuclockid2": (247),
    "ntp_gettime": (248),
    "minherit": (250),
    "rfork": (251),
    "issetugid": (253),
    "lchown": (254),
    "aio_read": (255),
    "aio_write": (256),
    "lio_listio": (257),
    "getdents": (272),
    "lchmod": (274),
    "lutimes": (276),
    "nstat": (278),
    "nfstat": (279),
    "nlstat": (280),
    "preadv": (289),
    "pwritev": (290),
    "fhstatfs": (297),
    "fhopen": (298),
    "fhstat": (299),
    "modnext": (300),
    "modstat": (301),
    "modfnext": (302),
    "modfind": (303),
    "kldload": (304),
    "kldunload": (305),
    "kldfind": (306),
    "kldnext": (307),
    "kldstat": (308),
    "kldfirstmod": (309),
    "getsid": (310),
    "setresuid": (311),
    "setresgid": (312),
    "aio_return": (314),
    "aio_suspend": (315),
    "aio_cancel": (316),
    "aio_error": (317),
    # "aio_read": (318),
    # "aio_write": (319),
    # "lio_listio": (320),
    "yield": (321),
    "mlockall": (324),
    "munlockall": (325),
    "getcwd": (326),
    "sched_setparam": (327),
    "sched_getparam": (328),
    "sched_setscheduler": (329),
    "sched_getscheduler": (330),
    "sched_yield": (331),
    "sched_get_priority_max": (332),
    "sched_get_priority_min": (333),
    "sched_rr_get_interval": (334),
    "utrace": (335),
    "sendfile": (336),
    "kldsym": (337),
    "jail": (338),
    "nnpfs_syscall": (339),
    # "sigprocmask": (340),
    # "sigsuspend": (341),
    # "sigaction": (342),
    # "sigpending": (343),
    # "sigreturn": (344),
    "sigtimedwait": (345),
    "sigwaitinfo": (346),
    "__acl_get_file": (347),
    "__acl_set_file": (348),
    "__acl_get_fd": (349),
    "__acl_set_fd": (350),
    "__acl_delete_file": (351),
    "__acl_delete_fd": (352),
    "__acl_aclcheck_file": (353),
    "__acl_aclcheck_fd": (354),
    "extattrctl": (355),
    "extattr_set_file": (356),
    "extattr_get_file": (357),
    "extattr_delete_file": (358),
    "aio_waitcomplete": (359),
    "getresuid": (360),
    "getresgid": (361),
    "kqueue": (362),
    "kevent": (363),
    "extattr_set_fd": (371),
    "extattr_get_fd": (372),
    "extattr_delete_fd": (373),
    "__setugid": (374),
    "eaccess": (376),
    "afs3_syscall": (377),
    "nmount": (378),
    "__mac_get_proc": (384),
    "__mac_set_proc": (385),
    "__mac_get_fd": (386),
    "__mac_get_file": (387),
    "__mac_set_fd": (388),
    "__mac_set_file": (389),
    "kenv": (390),
    "lchflags": (391),
    "uuidgen": (392),
    # "sendfile": (393),
    "mac_syscall": (394),
    # "getfsstat": (395),
    # "statfs": (396),
    # "fstatfs": (397),
    # "fhstatfs": (398),
    "ksem_close": (400),
    "ksem_post": (401),
    "ksem_wait": (402),
    "ksem_trywait": (403),
    "ksem_init": (404),
    "ksem_open": (405),
    "ksem_unlink": (406),
    "ksem_getvalue": (407),
    "ksem_destroy": (408),
    "__mac_get_pid": (409),
    "__mac_get_link": (410),
    "__mac_set_link": (411),
    "extattr_set_link": (412),
    "extattr_get_link": (413),
    "extattr_delete_link": (414),
    "__mac_execve": (415),
    # "sigaction": (416),
    # "sigreturn": (417),
    "getcontext": (421),
    "setcontext": (422),
    "swapcontext": (423),
    "swapoff": (424),
    "__acl_get_link": (425),
    "__acl_set_link": (426),
    "__acl_delete_link": (427),
    "__acl_aclcheck_link": (428),
    "sigwait": (429),
    "thr_create": (430),
    "thr_exit": (431),
    "thr_self": (432),
    "thr_kill": (433),
    "jail_attach": (436),
    "extattr_list_fd": (437),
    "extattr_list_file": (438),
    "extattr_list_link": (439),
    "ksem_timedwait": (441),
    "thr_suspend": (442),
    "thr_wake": (443),
    "kldunloadf": (444),
    "audit": (445),
    "auditon": (446),
    "getauid": (447),
    "setauid": (448),
    "getaudit": (449),
    "setaudit": (450),
    "getaudit_addr": (451),
    "setaudit_addr": (452),
    "auditctl": (453),
    "_umtx_op": (454),
    "thr_new": (455),
    "sigqueue": (456),
    "kmq_open": (457),
    "kmq_setattr": (458),
    "kmq_timedreceive": (459),
    "kmq_timedsend": (460),
    "kmq_notify": (461),
    "kmq_unlink": (462),
    "abort2": (463),
    "thr_set_name": (464),
    "aio_fsync": (465),
    "rtprio_thread": (466),
    "sctp_peeloff": (471),
    "sctp_generic_sendmsg": (472),
    "sctp_generic_sendmsg_iov": (473),
    "sctp_generic_recvmsg": (474),
    # "pread": (475),
    # "pwrite": (476),
    "mmap2": (477),
    # "lseek": (478),
    # "truncate": (479),
    # "ftruncate": (480),
    "thr_kill2": (481),
    "shm_open": (482),
    "shm_unlink": (483),
    "cpuset": (484),
    "cpuset_setid": (485),
    "cpuset_getid": (486),
    "cpuset_getaffinity": (487),
    "cpuset_setaffinity": (488),
    "faccessat": (489),
    "fchmodat": (490),
    "fchownat": (491),
    "fexecve": (492),
    "fstatat": (493),
    "futimesat": (494),
    "linkat": (495),
    "mkdirat": (496),
    "mkfifoat": (497),
    "mknodat": (498),
    "openat": (499),
    "readlinkat": (500),
    "renameat": (501),
    "symlinkat": (502),
    "unlinkat": (503),
    "posix_openpt": (504),
    "gssd_syscall": (505),
    "jail_get": (506),
    "jail_set": (507),
    "jail_remove": (508),
    "closefrom": (509),
    # "__semctl": (510),
    # "msgctl": (511),
    # "shmctl": (512),
    "lpathconf": (513),
    "__cap_rights_get": (515),
    "cap_enter": (516),
    "cap_getmode": (517),
    "pdfork": (518),
    "pdkill": (519),
    "pdgetpid": (520),
    "pselect": (522),
    "getloginclass": (523),
    "setloginclass": (524),
    "rctl_get_racct": (525),
    "rctl_get_rules": (526),
    "rctl_get_limits": (527),
    "rctl_add_rule": (528),
    "rctl_remove_rule": (529),
    "posix_fallocate": (530),
    "posix_fadvise": (531),
    "wait6": (532),
    "cap_rights_limit": (533),
    "cap_ioctls_limit": (534),
    "cap_ioctls_get": (535),
    "cap_fcntls_limit": (536),
    "cap_fcntls_get": (537),
    "bindat": (538),
    "connectat": (539),
    "chflagsat": (540),
    "accept4": (541),
    "pipe2": (542),
    "aio_mlock": (543),
    "procctl": (544),
    "ppoll": (545),
    "futimens": (546),
    "utimensat": (547),
    "fdatasync": (550),
    # "fstat": (551),
    # "fstatat": (552),
    # "fhstat": (553),
    # "getdirentries": (554),
    # "statfs": (555),
    # "fstatfs": (556),
    # "getfsstat": (557),
    # "fhstatfs": (558),
    # "mknodat": (559),
    # "kevent": (560),
    "cpuset_getdomain": (561),
    "cpuset_setdomain": (562),
    "getrandom": (563),
    "getfhat": (564),
    "fhlink": (565),
    "fhlinkat": (566),
    "fhreadlink": (567),
    "funlinkat": (568),
    "copy_file_range": (569),
    "__sysctlbyname": (570),
    "shm_open2": (571),
    "shm_rename": (572),
    "sigfastblock": (573),
    "__realpathat": (574),
    "close_range": (575),
    "rpctls_syscall": (576),
}